<?php

	$pid = $_GET['auction_id'];
	global $error;
	
	//-------------------------------------------


	if(isset($_POST['auction_submit_photos']))
	{
		
		$arr = $_POST['custom_field_id'];
		for($i=0;$i<count($arr);$i++)
		{
			$ids 	= $arr[$i];
			$value 	= $_POST['custom_field_value_'.$ids];
			
			if(is_array($value))
			{
				delete_post_meta($pid, "custom_field_ID_".$ids);
				
				for($j=0;$j<count($value);$j++) {
					add_post_meta($pid, "custom_field_ID_".$ids, $value[$j]);
					
				}
			}
			else
			update_post_meta($pid, "custom_field_ID_".$ids, $value);
			
		}
	
		
		wp_redirect(AuctionTheme_post_new_with_pid_stuff_thg($pid, 3));
		exit;	
	}


	if(isset($_POST['auction_submit_1']))
	{
		$auctionOK 				= 1;
		$auction_title 			= trim(strip_tags($_POST['auction_title']));
		$auction_description 	= nl2br(strip_tags($_POST['auction_description']));
		$auction_tags 			= trim(strip_tags($_POST['auction_tags']));
		
		$start_price 			= auctionTheme_clear_sums_of_cash(trim($_POST['start_price']));
		$buy_now 				= auctionTheme_clear_sums_of_cash(trim($_POST['buy_now']));
		$reserve 				= auctionTheme_clear_sums_of_cash(trim($_POST['reserve']));
		$shipping 				= auctionTheme_clear_sums_of_cash(trim($_POST['shipping']));
		
		$price 					= auctionTheme_clear_sums_of_cash(trim($_POST['price']));
		$auction_location_addr 	= strip_tags(trim($_POST['auction_location_addr']));
		
		$auction_category 		= $_POST['auction_cat_cat'];
		$auction_location 		= $_POST['auction_location_cat'];
		
		$do_not_require_shipping = $_POST['do_not_require_shipping'];
		
		if(!empty($do_not_require_shipping)) update_post_meta($pid, 'do_not_require_shipping', "1");
		else update_post_meta($pid, 'do_not_require_shipping', "0");
	
		//-----------------------------------------------------------------------------
		
			do_action('AuctionTheme_step_1_submit');
		
		//-----------------------------------------------------------------------------
		
		$auction_title 			= apply_filters('AuctionTheme_filter_auction_title', 			$auction_title, $pid); 
		$auction_description 	= apply_filters('AuctionTheme_filter_auction_description', 		$auction_description, $pid); 
		$auction_tags 			= apply_filters('AuctionTheme_filter_auction_tags', 			$auction_tags, $pid); 
		$start_price 			= apply_filters('AuctionTheme_filter_auction_start_price', 		$start_price, $pid); 
		$buy_now 				= apply_filters('AuctionTheme_filter_auction_buy_now_price', 	$buy_now, $pid); 
		$reserve 				= apply_filters('AuctionTheme_filter_auction_reserve_price', 	$reserve, $pid); 
		$shipping 				= apply_filters('AuctionTheme_filter_auction_shipping', 		$shipping, $pid); 
		$price 					= apply_filters('AuctionTheme_filter_auction_price', 			$price, $pid); 
		$auction_location_addr	= apply_filters('AuctionTheme_filter_auction_address', 			$auction_location_addr, $pid); 
		
		
		//-----------------------------------------------------------------------------
		
		$term = get_term( $auction_category, 'auction_cat' );	
		$auction_category = $term->slug;
				
		$term = get_term( $auction_location, 'auction_location' );	
		$auction_location = $term->slug;
		
		
		wp_set_object_terms($pid, array($auction_category),'auction_cat');
		wp_set_object_terms($pid, array($auction_location),'auction_location');		
		
		//-----------------------------------------------------------------------------
		
		$quant = trim($_POST['quant']);
		if(empty($quant) || !is_numeric($quant) || $quant < 0) $quant = 1;
		
		
		if(empty($auction_starting_price)) $auction_starting_price = 0;	
		
		//-------------------------------
		
	
		if(empty($auction_title)) 		{ $auctionOK = 0; $error['title'] 			= __('You cannot leave the auction title blank!','AuctionTheme'); }
		if(empty($auction_description)) { $auctionOK = 0; $error['description'] 	= __('You cannot leave the auction description blank!','AuctionTheme'); }
		
		if(empty($auction_category)) 		{ $auctionOK = 0; $error['category'] 		= __('You cannot leave the auction category blank!','AuctionTheme'); }
		
		$AuctionTheme_enable_locations = get_option('AuctionTheme_enable_locations');
		if($AuctionTheme_enable_locations != 'no')	
		if(empty($auction_location)) 		{ $auctionOK = 0; $error['location'] 		= __('You cannot leave the auction location blank!','AuctionTheme'); }
		
		

		$coupon = trim($_POST['coupon']);
		if(!empty($coupon))
		{
			$cup = auctionTheme_is_valid_cupoun($coupon);
			$_SESSION['coupon'] = $cup;
			
			if($cup == false) {$false_cup = 0; $ok_ad = 0; }
			
		}
		
		//----------------------------
		
		if(isset($_POST['featured']))
		{ 
			update_post_meta($pid, "featured", "1");
			$end = get_option('auctionTheme_ad_period_featured');
		}
		else update_post_meta($pid, "featured", "0");
		
		update_post_meta($pid, "closed", "0");
		update_post_meta($pid, "closed_date", "0");

		$my_post = array();
		$my_post['post_title'] 		= substr($auction_title,0,80);
		$my_post['ID'] 				= $pid;
		$my_post['post_content'] 	= $auction_description;	
		$my_post['post_status'] 	= 'draft';	
		wp_update_post( $my_post );
		
		
			
		//----------------------------------------------------------------
			// set categories
		
		//----------------------------------------------------------------
		
			wp_set_post_tags($pid, $auction_tags);
			  
			update_post_meta($pid, "Location", $auction_location_addr);
			update_post_meta($pid, "price", $price);
			  
			update_post_meta($pid, "paid", "0");
			update_post_meta($pid, "quant", $quant);			  
			  
			update_post_meta($pid, "shipping", 		empty($shipping) ? 0 : $shipping);
			update_post_meta($pid, "start_price", 	$start_price);
			update_post_meta($pid, "reserve", 		$reserve);
			
			update_post_meta($pid, "buy_now", 		$buy_now); 
			update_post_meta($pid, "reverse", 		$reverse); 
			update_post_meta($pid, "private_bids", 	strip_tags($_POST['private_bids'])); 
			update_post_meta($pid, "views", 		'0');
			update_post_meta($pid, "only_buy_now", 	(isset($_POST['only_buy_now']) ? "1" : "0" ));
			 
			$end 	= $_POST['ending']; 
			$nowtm 	= current_time('timestamp',0);
			
			//----- setting ending period ------------
			
			$ending = strtotime($end, $nowtm);
			
			$auctionTheme_auction_period = get_option('auctionTheme_auction_period');
			if(empty($auctionTheme_auction_period)) $auctionTheme_auction_period = 30;
			
			$auctionTheme_auction_period_featured = get_option('auctionTheme_auction_period_featured');
			if(empty($auctionTheme_auction_period_featured)) $auctionTheme_auction_period_featured = 30;
			
			$is_auction_featured = get_post_meta($pid, 'featured', true);
			if($is_auction_featured == "1") $time_ending = $nowtm + $auctionTheme_auction_period_featured *3600*24;
			else $time_ending = $nowtm + $auctionTheme_auction_period *3600*24;
			
			if(!empty($end)) 
			{
				$ending = strtotime($end, $nowtm);
				if($ending > $time_ending) $ending = $time_ending;
				
			} else $ending = $time_ending;
			
			//----------------------------------------
			
			update_post_meta($pid, "closed", 		"0");
			update_post_meta($pid, "closed_date", 	"0");
			update_post_meta($pid, "ending", 		$ending);
			 
			  
			  $reverse = get_post_meta($pid, "reverse", true);
			  
			  if($reverse == "yes")
			  {
			  		update_post_meta($pid, "current_bid", $price);
			  }
			  else
			  {
			  		update_post_meta($pid, "current_bid", $start_price);
					if(empty($start_price))
					{ 
						if(!empty($buy_now))
						update_post_meta($pid, "current_bid", $buy_now);
						else update_post_meta($pid, "current_bid", 0);
			  		}
			  }
			  
			  
			  
			  if(!empty($reserve))
			  {
					if(!empty($buy_now))
					{
						if($reserve >= $buy_now) { $auctionOK = 0; $error['reserve'] = __('Buy now price must be greater than reserve price.','AuctionTheme'); }	
					}
				  
			  }
			  
			  
			  if(!empty($buy_now))
			  {
					if(!empty($start_price))
					{
						if($buy_now <= $start_price) { $auctionOK = 0; $error['buy_now'] = __('Buy now price must be greater than start price.','AuctionTheme'); }	
					}
				  
			  }
			  
			  //------------------------------------------------------
			  $zip = get_post_meta($pid, "Location", true);
			  
			  $loc 		= wp_get_post_terms( $pid, 'auction_location');
			  $loc_a 	= '';
			 
			  foreach($loc as $l)
			 	$loc_a .= $l->name.',' ;
				
			  $loc_a .= $zip;
			  
			  $data = AuctionTheme_get_geo_coordinates($loc_a);
			  $long = $data[3];
			  $lat 	= $data[2];			  
			  
			  update_post_meta($pid, 'auction_lat', 	$lat);
			  update_post_meta($pid, 'auction_long', 	$long);
			  
			 // print_r($data);
			  //exit;
			  //------------------------------------------------------
		
		if($auctionOK == 1) //if everything ok, go to next step
		{		
			wp_redirect(AuctionTheme_post_new_with_pid_stuff_thg($pid, 2));
			exit;	
		}
		
	}


?>